package com.hotel.servlet;

import com.hotel.bean.Account;
import com.hotel.dao.AccountDao;
import com.hotel.util.DBOperation;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/ChangeInfoServlet")
public class ChangeInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        Account account = (Account) request.getSession().getAttribute("curUser");
        account.setUsername(request.getParameter("n_user"));
        account.setPhone(request.getParameter("n_phone"));
        account.setEmail(request.getParameter("n_mail"));
        account.setSex(request.getParameter("n_sex"));
        account.setAddress(request.getParameter("n_address"));
        account.setPostcode(request.getParameter("n_postcode"));
        account.setAccount_id(account.getAccount_id());
        System.out.println(account);
        SqlSession sqlSession = DBOperation.getSqlSession();
        //新建实体类
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
        accountDao.updateAccount(account);
        sqlSession.close();
     request.getSession().setAttribute("curUser",account);
        request.getRequestDispatcher("/jsp/wodexinxi.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
